package com.plantplaces.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.fs131046plantplaces.R;
import com.plantplaces.dto.Plant;
import com.plantplaces.dto.PlantSearch;
import com.plantplaces.service.IPlantService;
import com.plantplaces.service.PlantService;

public class PlantResultActivity extends ListActivity {
	
	// declare our DAO as interface type.
	IPlantService plantService;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		// instantiate our DAO.
		plantService = new PlantService(this);
		
		String searchTerm = this.getIntent().getStringExtra(AdvancedSearchActivity.PLANT_SEARCH_NAME);
	
		// create a PlantSearch object that I will pass to the DAO.
		PlantSearch ps = new PlantSearch();
		ps.setCommon(searchTerm);
		
		try {

			// create a new PlantSearchTask object.
			PlantSearchTask pst = new PlantSearchTask();
			
			// tell it to search for plants in a new thread.
			pst.execute(ps);
			
			int i = 1 + 1;
			
			int j = 2 + 2;
			
		} catch (Exception e) {
			Toast.makeText(this, getString(R.string.lblUnableToFetchPlants) + e.getMessage(), Toast.LENGTH_LONG).show();
		}
		
	}
	
	/**
	 * This method will get called when the user clicks an option from the list activity
	 */
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// which item did the user click?
		Plant selectedPlant = (Plant) getListAdapter().getItem(position);

		// the ID of the plant the user chose.
		int plantId = selectedPlant.getId();		
		
		// create our intent to go to the next screen.
		Intent plantDetailsIntent = new Intent(this, PlantDetailsActivity.class);
		
		// put the plant ID in the intent.
		plantDetailsIntent.putExtra(PlantPlacesActivity.PLANT_ID, plantId);
		
		// start the intent
		startActivity(plantDetailsIntent);
		
	}
	
	class PlantSearchTask extends AsyncTask<PlantSearch, Integer, List<Plant>> {

		/**
		 * This method will be called when the separate thread is finished, and it will
		 * marry back the results from the separate thread into our UI.
		 */
		@Override
		protected void onPostExecute(List<Plant> allPlants) {

			// An ArrayAdapter makes our plants available to the UI.
			ArrayAdapter<Plant> plantArrayAdapter = new ArrayAdapter<Plant>(PlantResultActivity.this, android.R.layout.simple_list_item_1, allPlants);
			
			// tell the layout about this array adapter
			setListAdapter(plantArrayAdapter);
		}
	
		/**
		 * This logic will run as a separate thread.
		 */
		@Override
		protected List<Plant> doInBackground(PlantSearch... ps) {
			// give me a list of plants that match the search criteria.
			List<Plant> allPlants = new ArrayList<Plant>();
			
			allPlants = plantService.fetchPlants(ps[0]);
			
			// TODO Auto-generated method stub
			return allPlants;
		}
		
	}
	

}
